<template>
  <div class="app-container">
    <!--  查询条件-->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="犯人编号" prop="UserId">
        <el-input
          v-model="queryParams.UserId"
          placeholder="请输入查询编号"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="犯人姓名" prop="UserName">
        <el-input
          v-model="queryParams.UserName"
          placeholder="请输入查询姓名"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <!--  按钮(新增，删除，导入，导出)-->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="triggerFileInput"
        >导入</el-button>
      </el-col>
      <input type="file" ref="fileInput" accept=".xlsx, .xls" @change="submitUpload" style="display:none;" />
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>
    <!--表格-->
    <el-table v-loading="loading" :data="UserList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column label="编号" align="center" key="UserId" prop="UserId" v-if="columns[0].visible" />
      <el-table-column label="姓名" align="center" key="UserName" prop="UserName" v-if="columns[1].visible" :show-overflow-tooltip="true" />
      <el-table-column label="性别" align="center" key="Sex" prop="Sex" v-if="columns[2].visible" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span>{{ scope.row.Sex === 0 ? '男' : '女' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="照片" align="center" key="PictureData" prop="PictureData" v-if="columns[3].visible" :show-overflow-tooltip="true" />

      <!--      <el-table-column label="创建时间" align="center" prop="createTime" v-if="columns[4].visible" width="160">-->
      <!--        <template slot-scope="scope">-->
      <!--          <span>{{ parseTime(scope.row.createTime) }}</span>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <el-table-column
        label="操作"
        align="center"
        width="160"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--分页-->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!--    添加或修改犯人信息的对话框-->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-row :gutter="15">
        <el-form ref="elForm" :model="formData" :rules="rules" size="medium" label-width="100px">
          <el-col :span="12">
            <el-form-item label="犯人编号" prop="UserId">
              <el-input v-model="formData.UserId" placeholder="请输入犯人编号" :maxlength="20" clearable
                        :style="{width: '100%'}"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="犯人姓名" prop="UserName">
              <el-input v-model="formData.UserName" placeholder="请输入犯人姓名" :maxlength="20" clearable
                        :style="{width: '100%'}"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="犯人性别" prop="Sex">
              <el-select v-model="formData.Sex" placeholder="请选择犯人性别" clearable :style="{width: '100%'}">
                <el-option v-for="(item, index) in SexOptions" :key="index" :label="item.label"
                           :value="item.value" :disabled="item.disabled"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="犯人照片名" prop="PictureData">
              <el-input v-model="formData.PictureData" placeholder="请输入照片名" :maxlength="20" clearable
                        :style="{width: '100%'}"></el-input>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {getUserList,delUser,addUser,updateUser,getExportUserList} from '@/api/rollCall/people'
import { getToken } from '@/utils/auth'
import * as XLSX from 'xlsx';
import { saveAs } from 'file-saver';
export default {
  name: "people",
  data() {
    return {
      //查询
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        UserId: undefined,
        UserName: undefined,
      },
      //文件上传部分
      uploadUrl: `${process.env.VUE_APP_BASE_API}/User/uploadUserInfo`,
      exportUserList: [],
      // 显示搜索条件
      showSearch: true,
      // 遮罩层
      loading: true,
      // 非多个禁用
      multiple: true,
      // 选中数组
      ids: [],
      // 列信息
      columns: [
        { key: 0, label: `编号`, visible: true },
        { key: 1, label: `姓名`, visible: true },
        { key: 2, label: `性别`, visible: true },
        { key: 3, label: `照片`, visible: true },
      ],
      // 总条数
      total: 2,
      // 用户表格数据
      UserList: null,
      //弹窗
      // 弹出层标题
      title: "",
      open: false,
      formData: {
        UserId: undefined,
        UserName: undefined,
        Sex: undefined,
        UserPart: undefined,
        PictureData: undefined,
      },
      rules: {
        UserId: [{
          required: true,
          message: '请输入犯人编号',
          trigger: 'blur'
        }],
        UserName: [{
          required: true,
          message: '请输入犯人姓名',
          trigger: 'blur'
        }],
        Sex: [{
          required: true,
          message: '请选择犯人性别',
          trigger: 'change'
        }],
        PictureData: [{
          required: true,
          message: '请输入犯人照片名',
          trigger: 'blur'
        }]
      },
      SexOptions: [{
        "label": "男",
        "value": 0
      }, {
        "label": "女",
        "value": 1
      }],
      UserPartOptions: [],
      UserPartProps: {},
      //判断是新增还是修改(新增0，修改1)
      state:0,
    };
  },
  created() {
    this.getList()
  },
  mounted() {
    const XLSX = require('xlsx');  // 动态引入
  },
  methods: {
    //文件上传
    // 触发文件选择框
    triggerFileInput() {
      this.$refs.fileInput.click();
    },
    // 处理文件上传
    submitUpload(event) {
      console.log('处理文件上传')
      const selectedFile = event.target.files[0];
      if (!selectedFile) {
        console.log('没有文件被选择，什么也不做')
        return; // 如果没有文件被选择，什么也不做
      }
      const formData = new FormData();
      formData.append('file', selectedFile);

      // 发送文件上传请求
      fetch(this.uploadUrl, {
        method: 'POST',
        headers: {
          // 如果需要 token，这里可以添加 Authorization
          'Authorization': 'Bearer ' + getToken(),
        },
        body: formData,
      })
        .then(response => {
          console.log('response')
          console.log(response.json())
          this.$refs.fileInput.value = null; // 清空文件输入
          this.getList()
        })
        .then(data => {
          this.$message.success('文件上传成功');
        })
        .catch(error => {
          this.$message.error('文件上传失败');
          console.error(error);
          this.$refs.fileInput.value = null;
        });
    },
    //点击导出操作 1.获取要导出的数据
    handleExport() {
      getUserList(this.queryParams).then(res => {
        console.log(res)
        this.exportUserList = res.data;
        this.submitExport()
      })
    },
    // 2. 将获取到的数据转成excel
    submitExport() {
      const sheetData = [
        ['用户编号', '用户姓名', '用户性别', '用户部门', '用户照片名'], // 表头
        ...this.exportUserList.map(item => [
          item.UserId,
          item.UserName,
          item.Sex === '0' ? '男' : '女', // 性别转换为可读格式
          item.UserPart,
          item.PictureData
        ])
      ];
      // 创建工作簿
      const wb = XLSX.utils.book_new();
      // 使用 aoa_to_sheet 将二维数组数据转换为工作表
      const ws = XLSX.utils.aoa_to_sheet(sheetData);
      // 添加工作表到工作簿
      XLSX.utils.book_append_sheet(wb, ws, 'Sheet1');
      // 导出为二进制数据
      const wbout = XLSX.write(wb, { bookType: 'xlsx', type: 'array' });
      // 保存文件为 .xlsx 格式
      saveAs(new Blob([wbout], { type: 'application/octet-stream' }), '人员信息.xlsx');
    },
    //查询--------------------------------------------
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    //按钮（新增、删除、导入、导出）------------------------
    /** 新增按钮操作 */
    handleAdd() {
      this.state = 0
      this.reset();
      this.open = true;
      this.title = "添加用户";
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const UserIds = row.UserId || this.ids;
      console.log(UserIds)
      this.$modal.confirm('是否确认删除犯人编号为"' + UserIds + '"的数据项？').then(function() {
        //******************************
        return delUser(UserIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    //表格-----------------------------------------------
    // /** 查询用户列表 */
    getList() {
      console.log(this.queryParams)
      getExportUserList(this.queryParams).then(res => {
          console.log(res)
          this.UserList = res.rows;
          this.total = res.total;
          this.loading = false;
        }
      );
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.UserId);
      this.multiple = !selection.length;
      console.log(this.ids)
    },

    //弹窗-------------------------------------------------
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.state=1
      this.reset();
      console.log(row)
      this.formData.UserId = row.UserId
      this.formData.UserName = row.UserName
      this.formData.Sex = row.Sex
      this.formData.UserPart = row.UserPart
      this.formData.PictureData = row.PictureData
      this.open = true;
      this.title = "修改用户";
    },
    /** 提交按钮 */
    submitForm: function() {
      this.formData.UserPart = "部门一"
      console.log(this.formData)
      this.$refs["elForm"].validate(valid => {
        if (valid) {
          if (this.state == 1) {
            // this.formData.UserPart='yi'
            console.log(this.formData)
            updateUser(this.formData).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            // this.formData.UserPart='yi'
            console.log(this.formData)
            //我在这里传的！
            addUser(this.formData).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
// 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.formData = {
        UserId: undefined,
        UserName: undefined,
        Sex: undefined,
        UserPart: undefined,
        PictureData: undefined,
      };
      this.resetForm("elForm");
    },
  }
};
</script>
